<?php

/**
* -------------------------------------------------------------------------------------------------------------------
* @abstract   Handles the CRUD operations for Operator model and other methods
* @package    Kiropedic
* @subpackage Controllers
* @author     Darwing Hernández - darwing.her69@gmail.com
* -------------------------------------------------------------------------------------------------------------------
*/

class OperatorController extends BaseController
{

  /**
  * ---------------------------------------------------------------------------------------------------------------
  * ATTRIBUTES AND CONSTANTS
  * ---------------------------------------------------------------------------------------------------------------
  */
  public static $restful = true;


  /**
  * ---------------------------------------------------------------------------------------------------------------
  * VIEWS
  * ---------------------------------------------------------------------------------------------------------------
  */





  /**
  * ---------------------------------------------------------------------------------------------------------------
  * CRUD OPERATIONS
  * ---------------------------------------------------------------------------------------------------------------
  */

  /**
  * CREATE Operation
  * @abstract creates a new user record
  * @param    int - mode to create the user (1: public, 2: admin)
  * @return   View
  * @access   public
  */
  public function register()
  {
    $input  = Input::all();
    $index  = array('cedula', 'email', 'login');
    $values = array('unique:operator_ope,ope_rifci', 
                    'unique:operator_ope,ope_email', 
                    'unique:operator_ope,ope_login');

    if (!$this->validate($input, CREATE_OPER, $index, $values))
      return Redirect::to('admin/create/oper')->withErrors($this->validation)->withInput()->with('notification', 'register_add_warning')->with('opt', 'El operador ');

    $name = '#';

    if(Input::hasFile('foto'))
    {
      $name = str_replace(array('/', '.'), '_', Hash::make(Input::get('login').Input::get('cedula'))).'.'.Input::file('foto')->getClientOriginalExtension();
      Input::file('foto')->move('images', $name);
    }
    
    $operator = Operator::create(array(
      'ope_name'    =>  $input['nombre'],
      'ope_login'   =>  $input['login'],
      'ope_rifci'   =>  $input['cedula'],
      'ope_email'   =>  $input['email'],
      'ope_phone'   =>  $input['telefono'],
      'ope_active'  =>  $input['active'] == "on"? 1: 0,
      'ope_image'   =>  $name
    ));

    if(Input::get('again') == "on")
      return Redirect::to('admin/create/oper')->with('notification', "register_add_success")->with('opt', 'El operador ');
    else
      return Redirect::to('admin/list/listoper')->with('notification', "register_add_success")->with('opt', 'El operator ');
  }

  /**
  * READ Operation
  * @abstract  Reads a record by id and shows the related data
  * @param     int - record ID
  * @return    View
  * @access    public
  */
  public function view($id)
  {
    $operator = Operator::select('ope_code', 'ope_name', 'ope_login', 'ope_rifci', 'ope_email', 'ope_phone', 'ope_active', 'ope_image')
                        ->find($id);

    if(!$operator) Redirect::to('admin/list/listoper')->with('notification', 'not_allowed');

    if($operator->ope_image == '#') $operator->ope_image = 'default-avatar.png';
    return View::make('admin.operator.editOper')->with('operator', $operator)->with('title', 'Editar Operador')->with('active', 3); 
  }

  /**
  * UPDATE Operation
  * @abstract  updates a record by id
  * @param     int - record ID
  * @return    View
  * @access    public
  */
  public function update($id)
  {
    $input  = Input::all();
    $index  = array('cedula', 'email', 'login');
    $values = array('unique:operator_ope,ope_rifci,'.$id.',ope_code', 
                    'unique:operator_ope,ope_email,'.$id.',ope_code', 
                    'unique:operator_ope,ope_login,'.$id.',ope_code');

    if (!$this->validate($input, UPDATE_OPER, $index, $values))
      return  Redirect::to('admin/viewOper/'.$id)
                      ->with('notification', 'register_edit_warning')
                      ->with('opt', 'El operador ')
                      ->withErrors($this->validation)
                      ->withInput();

    $name = Input::get('imgvalue');
    if(Input::hasFile('foto'))
    {
      if($name != 'default-avatar.png') File::delete('images/'.$name);
      $name = str_replace(array('/', '.'), '_', Hash::make(Input::get('login').Input::get('cedula'))).'.'.Input::file('foto')->getClientOriginalExtension();
      Input::file('foto')->move('images', $name);
    }

    $operator             = Operator::find($id);
    $operator->ope_name   = Input::get('nombre');
    $operator->ope_login  = Input::get('login');
    $operator->ope_rifci  = Input::get('cedula');
    $operator->ope_email  = Input::get('email');
    $operator->ope_phone  = Input::get('telefono');
    $operator->ope_active = Input::get('active') == "on"? 1: 0;
    $operator->ope_image  = $name;
    $operator->save();

    return Redirect::to('admin/viewOper/'.$id)->with('notification', "register_edit_success")->with('opt', 'El operador ');
  }

  /**
  * DELETE Operation
  * @abstract deletes records by specified IDs
  * @param    array - all record ids to be deleted
  * @access   public
  */
  public static function delete($keys)
  {
    Operator::destroy($keys);
    return Redirect::to('admin/list/listoper')->with('notification', 'register_delete_success');
  }

  /**
  * ---------------------------------------------------------------------------------------------------------------
  * OTHER METHODS/ACTIONS AND STUFF
  * ---------------------------------------------------------------------------------------------------------------
  */

}